'use strict'

const path = require('path')

module.exports = appInfo => {
  const prefix = 'xhx' // 文件夹
  const dbName = 'xhx_test' // 数据库
  const tablePrefix = 'yqfk' // 表前缀
  const storageDir =
    process.platform == 'linux' ? `/data/node/static/${prefix}` : path.join(__dirname, `../app/storage`)

  const config = {
    prefix,

    name: '疫情防控',

    host: 'http://127.0.0.1:7002',
    h5: 'http://localhost:3500/h5',
    client: 'http://127.0.0.1:3001',

    dbName,

    // 资源存储路径
    storageDir,

    // 上次文件路径
    uploadDir: storageDir + '/public',

    mongodb: `mongodb://root:FY8iDztMBKDIejpx@36.99.193.53:27017/${dbName}?authSource=admin`,

    // 注意，开启此模式后，应用就默认自己处于反向代理之后，
    // 会支持通过解析约定的请求头来获取用户真实的 IP，协议和域名。
    // 如果你的服务未部署在反向代理之后，请不要开启此配置，以防被恶意用户伪造请求 IP 等信息。
    proxy: true,
  }

  // Cookie签名秘钥
  config.keys = appInfo.name + '_1605061497228_1857'

  config.middleware = [
    // 'requestLog',
    'koaLogger',
  ]

  config.mongoose = {
    url: config.mongodb,
  }

  config.security = {
    csrf: false,
  }

  config.cors = {
    origin: ctx => ctx.headers.origin,
    credentials: true,
    // allowHeaders: 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With',
    // allowMethods: 'PUT,POST,GET,DELETE,OPTIONS',
  }

  config.static = {
    dir: [
      { prefix: '/', dir: config.uploadDir },
      {
        prefix: '/',
        dir: path.join(__dirname, '../app/public'),
      },
    ],
  }

  // jwt
  config.jwt = {
    secret: config.keys + 'jwt2021',
    // 有效期
    expiresIn: '14d',
  }

  config.session = {
    key: prefix + 'SESSION_ID',
    // 7天
    maxAge: 1000 * 60 * 60 * 24 * 7, // 毫秒
    httpOnly: true,
    encrypt: true,
    renew: true, // 延长会话有效期
  }

  config.sessionMongoose = {
    name: 'Session',
  }

  config.jax = {
    mongoose: {
      prefix: tablePrefix,
    },
  }

  config.dict = require('./dict')
  config.constant = require('./constant')

  // 西华测试
  config.mysql = {
    host: '36.99.193.53',
    user: 'root',
    password: 'TAeB8bmBvFG2LV~',
    database: 'xhyqfk_test',
    port: 5001,
  }

  config.knex = {
    client: 'mysql',
    connection: config.mysql,
    pool: { min: 0, max: 7 },
    debug: true,
  }

  config.logrotator = {
    maxFiles: 10,
    maxDays: 7,
  }

  return config
}
